package com.uh.summary.serviceImpl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.uh.calculation.bean.WarnEnterpriseRecord;
import com.uh.calculation.bean.WarnJiSuanBean;
import com.uh.calculation.inter.Warn;
import com.uh.calculation.inter.bathImp.BatchMaoyiImp;
import com.uh.calculation.inter.imp.*;
import com.uh.calculation.service.CalculationService;
import com.uh.microservice.core.dao.JdbcDao;
import com.uh.microservice.core.util.ThreadPool;
import com.uh.summary.entity.WarnBean;
import com.uh.summary.enums.*;
import com.uh.summary.mapper.BathMapper;
import com.uh.summary.mapper.WarnEnterpriseRecordMapper;
import com.uh.summary.service.IWarnEnterpriseRecordService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.EnumUtils;
import org.joda.time.DateTime;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;

@Service
@Slf4j
public class WarnEnterpriseRecordServiceImpl implements IWarnEnterpriseRecordService {
    @Resource
    private WarnEnterpriseRecordMapper warnEnterpriseRecordMapper;
    @Resource
    private CalculationService calculationService;

    private Warn warn;
    @Resource
    private BathMapper bathMapper;

    @Autowired
    private JdbcDao jdbcDao;


    @Override
    public int saveNoEditBatch(RuleTypeEnum ruleTypeEnum,String lastTime) {
        Integer start = warnEnterpriseRecordMapper.getMaxWarnCode();
        WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
            Date d = sdf.parse(lastTime);
            warnJiSuanBean.setLastTime(d);
        } catch (Exception e) {

        }
        warnJiSuanBean.setStartTime( System.currentTimeMillis());
        String  tabname="";
        switch (ruleTypeEnum) {
            case industrial:
                tabname="b204-1";
                warnJiSuanBean.setSubjectCode(TableNameEnum.B_TYPE.getType());
                break;//工业
            case investment:
                tabname="206";
                warnJiSuanBean.setSubjectCode(TableNameEnum._206_TYPE.getType());
                break;//投资
            case trade:
                tabname="e204-1";
                warnJiSuanBean.setSubjectCode(TableNameEnum.E_TYPE.getType());
                break;//贸易
            case trades:
                tabname="s204-1";
                warnJiSuanBean.setSubjectCode(TableNameEnum.E_TYPE.getType());
                break;//贸易
            case services:
                tabname="f203";
                warnJiSuanBean.setSubjectCode(TableNameEnum.F_TYPE.getType());
                break;//服务
        }
        warnJiSuanBean.setStart(start);
        warnJiSuanBean.setWarnEnterpriseRecordMapper(warnEnterpriseRecordMapper);
        warnJiSuanBean.setBathMapper(bathMapper);
        warnJiSuanBean.setJdbcDao(jdbcDao);
        Integer n = warnEnterpriseRecordMapper.getTabNum(tabname);
        warnJiSuanBean.setTotalNum(n);
        ThreadPoolExecutor pool  = ThreadPool.getInstance().getPool();
        //pool.shutdown();
        int stt=10000;
        for(int i=0;i<n;i += stt){
            try {

                while (true) {
                    if (warnJiSuanBean.getIsSelect()) {
                        break;
                    } else {
                        Thread.sleep(100);
                    }
                }
                warnJiSuanBean.setSt(i);
                warnJiSuanBean.setRuleType(ruleTypeEnum);
                warnJiSuanBean.setOffset(stt);
                warnJiSuanBean.setIsSelect(false);
                pool.submit(new BatchServerThread(warnJiSuanBean));
                System.out.println(i+ "   "+stt);
            }catch (Exception E){
                E.printStackTrace();
            }
        }

        isCompleted(pool);
        Long endTime = System.currentTimeMillis();
        Long tempTime = (endTime - warnJiSuanBean.getStartTime());
        System.out.println("处理完，用时："+
                (((tempTime/86400000)>0)?((tempTime/86400000)+"d"):"")+
                ((((tempTime/86400000)>0)||((tempTime%86400000/3600000)>0))?((tempTime%86400000/3600000)+"h"):(""))+
                ((((tempTime/3600000)>0)||((tempTime%3600000/60000)>0))?((tempTime%3600000/60000)+"m"):(""))+
                ((((tempTime/60000)>0)||((tempTime%60000/1000)>0))?((tempTime%60000/1000)+"s"):(""))+
                ((tempTime%1000)+"ms"));
        // 打印结果

        return 0;
    }








    @Override
    public int saveNoEditRecords(RuleTypeEnum ruleTypeEnum) {
        int num =20000;
        int st=0;
        WarnBean  warnBean = new WarnBean();
        final  RuleTypeEnum r=ruleTypeEnum;
        Integer start = warnEnterpriseRecordMapper.getMaxWarnCode();


        String  tabname="";


        switch (ruleTypeEnum) {
            case industrial:
                tabname="b204-1";
                warn = new GongyeImp();
                break;//工业
            case investment:
                tabname="206";
                warn = new GongyeImp();
                break;//投资
            case trade:
                tabname="e204-1";
                warn = new BatchMaoyiImp();
                break;//贸易
            case trades:
                tabname="s204-1";
                warn = new GongyeImp();
                break;//贸易
            case services:
                tabname="f203";
                warn = new GongyeImp();
                break;//服务

        }

        Integer n = warnEnterpriseRecordMapper.getTabNum(tabname);






        //getOrganization.getFieldName("E104-1", warnJiSuan.getYear(), warnJiSuan.getSurveyobjectid());


        int stt=8000;

        for(int i=0;i<(n+stt);){
            try {

                ThreadPool.getInstance().getPool().execute(new ServerThread(warnEnterpriseRecordMapper, calculationService, warnBean, i, i + stt, r, start));
                i += stt;
            }catch (Exception E){}
        }

        return 0;
    }

    private static void isCompleted(ThreadPoolExecutor threadPool) {
        //threadPool.shutdown();
        while (threadPool.getTaskCount() != threadPool.getCompletedTaskCount()) {
        }
    }


    //
    //@Override
    public int saveNoEditRecords_delete(RuleTypeEnum ruleType) {

        WarnJiSuanBean warnJiSuanBean = new WarnJiSuanBean();
        List<String> companyCodes = new ArrayList<>();
        Integer start = warnEnterpriseRecordMapper.getMaxWarnCode();
        //待插入
        List<WarnEnterpriseRecord> list = new ArrayList<>();
        switch (ruleType) {
            case industrial:
                int limit = 0;
                do {
                    list = warnEnterpriseRecordMapper.getIndustrial(limit, 100,null);
                    limit += 100;
                    for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
                        List<WarnEnterpriseRecord> qryList = new ArrayList<>();
                        warnJiSuanBean = new WarnJiSuanBean();
                        warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
                        warnJiSuanBean.setWarnEnterpriseRecords(qryList);
                        warnJiSuanBean.setSubjectCode(TableNameEnum.B_TYPE.getCode());
                        calculationService.count(warnJiSuanBean);
                        for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
                            record.setId(generateSnowFlake());
                            record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
                            record.setProvince(warnEnterpriseRecord.getProvince());
                            record.setCity(warnEnterpriseRecord.getCity());
                            record.setArea(warnEnterpriseRecord.getArea());
                            record.setCompanyName(warnEnterpriseRecord.getCompanyName());
                            record.setRegionArea(warnEnterpriseRecord.getRegionArea());
                            record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
                            record.setMajorClassify(TableNameEnum.B_TYPE.getType());
                            record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
                            if (record.getWarnType() != null) {
                                record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
                            } else {
                                record.setWarningIndex("0.0");
                            }
                            start++;
                        }
                        log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
                        if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
                            System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
                            //warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
                        }
                    }
                } while (!list.isEmpty());
                break;
            case investment:
                limit = 0;
                do {
                    list = warnEnterpriseRecordMapper.getInvest(limit, 100, null);
                    limit += 100;
                    for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
                        List<WarnEnterpriseRecord> qryList = new ArrayList<>();
                        warnJiSuanBean = new WarnJiSuanBean();
                        warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
                        warnJiSuanBean.setWarnEnterpriseRecords(qryList);
                        warnJiSuanBean.setSubjectCode(TableNameEnum._206_TYPE.getCode());
                        calculationService.count(warnJiSuanBean);
                        for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
                            record.setId(generateSnowFlake());
                            record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
                            record.setProvince(warnEnterpriseRecord.getProvince());
                            record.setCity(warnEnterpriseRecord.getCity());
                            record.setArea(warnEnterpriseRecord.getArea());
                            record.setCompanyName(warnEnterpriseRecord.getCompanyName());
                            record.setRegionArea(warnEnterpriseRecord.getRegionArea());
                            record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
                            record.setMajorClassify(TableNameEnum._206_TYPE.getType());
                            record.setProjectCode(warnEnterpriseRecord.getProjectCode());
                            record.setProjectName(warnEnterpriseRecord.getProjectName());
                            record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
                            if (record.getWarnType() != null) {
                                record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
                            } else {
                                record.setWarningIndex("0.0");
                            }
                            start++;
                        }
                        log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
                        if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
                            System.out.println(warnJiSuanBean.getWarnEnterpriseRecords());
                            //warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
                        }
                    }
                } while (!list.isEmpty());
                break;
            case trade:
                limit = 0;
                //贸易-E
                do {
                    list = warnEnterpriseRecordMapper.getTradeE(limit, 100, null);
                    limit += 100;
                    for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
                        List<WarnEnterpriseRecord> qryList = new ArrayList<>();
                        warnJiSuanBean = new WarnJiSuanBean();
                        warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
                        warnJiSuanBean.setWarnEnterpriseRecords(qryList);
                        warnJiSuanBean.setSubjectCode(TableNameEnum.E_TYPE.getCode());
                        calculationService.count(warnJiSuanBean);
                        for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
                            record.setId(generateSnowFlake());
                            record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
                            record.setProvince(warnEnterpriseRecord.getProvince());
                            record.setCity(warnEnterpriseRecord.getCity());
                            record.setArea(warnEnterpriseRecord.getArea());
                            record.setCompanyName(warnEnterpriseRecord.getCompanyName());
                            record.setRegionArea(warnEnterpriseRecord.getRegionArea());
                            record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
                            record.setMajorClassify(TableNameEnum.S_TYPE.getType());
                            record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
                            if (record.getWarnType() != null) {
                                record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
                            } else {
                                record.setWarningIndex("0.0");
                            }
                            start++;
                        }
                        log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
                        if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
                        }
                    }
                } while (!list.isEmpty());
                //贸易-S
                do {
                    list = warnEnterpriseRecordMapper.getTradeS(limit, 100, null);
                    limit += 100;
                    for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
                        List<WarnEnterpriseRecord> qryList = new ArrayList<>();
                        warnJiSuanBean = new WarnJiSuanBean();
                        warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
                        warnJiSuanBean.setWarnEnterpriseRecords(qryList);
                        warnJiSuanBean.setSubjectCode(TableNameEnum.S_TYPE.getCode());
                        calculationService.count(warnJiSuanBean);
                        for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
                            record.setId(generateSnowFlake());
                            record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
                            record.setProvince(warnEnterpriseRecord.getProvince());
                            record.setCity(warnEnterpriseRecord.getCity());
                            record.setArea(warnEnterpriseRecord.getArea());
                            record.setCompanyName(warnEnterpriseRecord.getCompanyName());
                            record.setRegionArea(warnEnterpriseRecord.getRegionArea());
                            record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
                            record.setMajorClassify(TableNameEnum.S_TYPE.getType());
                            record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
                            if (record.getWarnType() != null) {
                                record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
                            } else {
                                record.setWarningIndex("0.0");
                            }

                            start++;
                        }
                        log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
                        if (!warnJiSuanBean.getWarnEnterpriseRecords().isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
                        }
                    }
                } while (!list.isEmpty());
                break;
            case services:
                limit = 0;
                do {
                    list = warnEnterpriseRecordMapper.getService(limit, 100, null);
                    limit += 100;
                    for (WarnEnterpriseRecord warnEnterpriseRecord : list) {
                        List<WarnEnterpriseRecord> qryList = new ArrayList<>();
                        warnJiSuanBean = new WarnJiSuanBean();
                        warnJiSuanBean.setSurveyobjectid(warnEnterpriseRecord.getCompanyCode());
                        warnJiSuanBean.setWarnEnterpriseRecords(qryList);
                        warnJiSuanBean.setSubjectCode(TableNameEnum.F_TYPE.getCode());
                        calculationService.count(warnJiSuanBean);
                        for (WarnEnterpriseRecord record : warnJiSuanBean.getWarnEnterpriseRecords()) {
                            record.setId(generateSnowFlake());
                            record.setWarnCode(generateWarnCode(record.getDimension(), start, String.valueOf(new DateTime(record.getCreateTime()).getYear())));
                            record.setProvince(warnEnterpriseRecord.getProvince());
                            record.setCity(warnEnterpriseRecord.getCity());
                            record.setArea(warnEnterpriseRecord.getArea());
                            record.setCompanyName(warnEnterpriseRecord.getCompanyName());
                            record.setRegionArea(warnEnterpriseRecord.getRegionArea());
                            record.setCompanyCode(warnEnterpriseRecord.getCompanyCode());
                            record.setMajorClassify(TableNameEnum.F_TYPE.getType());
                            record.setWarnFactor(String.format("%.1f", Double.parseDouble(record.getWarnFactor())));
                            if (record.getWarnType() != null) {
                                record.setWarningIndex(String.format("%.1f", generateWarnIndex(record.getWarnType(), record.getWarnFactor())));
                            } else {
                                record.setWarningIndex("0.0");
                            }
                            start++;
                        }
                    }
                    log.info(">>>>>>>>>{}企业预警{}条", warnJiSuanBean.getSurveyobjectid(), warnJiSuanBean.getWarnEnterpriseRecords().size());
                    if (warnJiSuanBean.getWarnEnterpriseRecords().size() > 0) {
                        warnEnterpriseRecordMapper.insertBatch(warnJiSuanBean.getWarnEnterpriseRecords());
                    }
                } while (list.size() != 0);
                break;
        }
        return 0;
    }



    @Override
    public int saveEditRecords(RuleTypeEnum ruleTypeEnum, String years) {
        String[] yearList = years.split(",");
        List<WarnEnterpriseRecord> ls = new ArrayList<>();
        Integer start = warnEnterpriseRecordMapper.getMaxWarnCode();
        for (String year : yearList) {
            switch (ruleTypeEnum) {
                case industrial:
                    int limit = 0;
                    String statTableId = warnEnterpriseRecordMapper.selectStatIdBy(year, TableNameEnum.B_TYPE.getDes());
                    //需要计算风险的企业
                    List<WarnEnterpriseRecord> list = warnEnterpriseRecordMapper.selectSameIpBy("et_" + statTableId, year);
                    List<WarnEnterpriseRecord> res = new ArrayList<>();
                    do {
                        res = new ArrayList<>();
                        ls = warnEnterpriseRecordMapper.getIndustrial(limit, 100,null);
                        limit += 100;
                        for (WarnEnterpriseRecord record : list) {
                            for (WarnEnterpriseRecord rd : ls) {
                                if (record.getCompanyCode().equals(rd.getCompanyCode())) {
                                    WarnEnterpriseRecord warnEnterpriseRecord = new WarnEnterpriseRecord();
                                    BeanUtils.copyProperties(rd, warnEnterpriseRecord);
                                    warnEnterpriseRecord.setYearIsMonth(record.getYearIsMonth());
                                    warnEnterpriseRecord.setWarnFactor("30");
                                    warnEnterpriseRecord.setWarnType(RuleEnum.INDUSTRY_EDIT_IP_FACTOR_DATA.getCode());
                                    warnEnterpriseRecord.setDimension(DimensionEnum.A.getCode());
                                    warnEnterpriseRecord.setMajorClassify(TableNameEnum.B_TYPE.getType());
                                    warnEnterpriseRecord.setWarnCode(generateWarnCode(DimensionEnum.A.getCode(), start, year));
                                    warnEnterpriseRecord.setWarningIndex(String.format("%.1f", 30 * 0.15));
                                    warnEnterpriseRecord.setCreateTime(DateTime.now().toDate());
                                    start++;
                                    res.add(warnEnterpriseRecord);
                                    break;
                                }
                            }
                        }
                        if (!res.isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(res);
                        }
                    } while (!ls.isEmpty());
                    break;
                case trade:
                    limit = 0;
                    statTableId = warnEnterpriseRecordMapper.selectStatIdBy(year, TableNameEnum.E_TYPE.getDes());
                    //需要计算风险的企业 e贸易
                    list = warnEnterpriseRecordMapper.selectSameIpBy("et_" + statTableId, year);
                    do {
                        res = new ArrayList<>();
                        ls = warnEnterpriseRecordMapper.getTradeE(limit, 100, year);
                        limit += 100;
                        for (WarnEnterpriseRecord record : list) {
                            for (WarnEnterpriseRecord rd : ls) {
                                if (record.getCompanyCode().equals(rd.getCompanyCode())) {
                                    WarnEnterpriseRecord warnEnterpriseRecord = new WarnEnterpriseRecord();
                                    BeanUtils.copyProperties(rd, warnEnterpriseRecord);
                                    warnEnterpriseRecord.setYearIsMonth(record.getYearIsMonth());
                                    warnEnterpriseRecord.setWarnFactor("30");
                                    warnEnterpriseRecord.setWarnType(RuleEnum.TRADE_EDIT_IP_FACTOR_DATA.getCode());
                                    warnEnterpriseRecord.setDimension(DimensionEnum.A.getCode());
                                    warnEnterpriseRecord.setMajorClassify(TableNameEnum.E_TYPE.getType());
                                    warnEnterpriseRecord.setWarnCode(generateWarnCode(DimensionEnum.A.getCode(), start, year));
                                    warnEnterpriseRecord.setWarningIndex(String.format("%.1f", 30 * 0.15));
                                    warnEnterpriseRecord.setCreateTime(DateTime.now().toDate());
                                    start++;
                                    res.add(warnEnterpriseRecord);
                                    break;
                                }
                            }
                        }
                        if (!res.isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(res);
                        }
                    } while (!ls.isEmpty());
                    statTableId = warnEnterpriseRecordMapper.selectStatIdBy(year, TableNameEnum.S_TYPE.getDes());
                    //需要计算风险的企业 s贸易
                    list = warnEnterpriseRecordMapper.selectSameIpBy("et_" + statTableId, year);
                    do {
                        res = new ArrayList<>();
                        ls = warnEnterpriseRecordMapper.getTradeS(limit, 100, year);
                        limit += 100;
                        for (WarnEnterpriseRecord record : list) {
                            for (WarnEnterpriseRecord rd : ls) {
                                if (record.getCompanyCode().equals(rd.getCompanyCode())) {
                                    WarnEnterpriseRecord warnEnterpriseRecord = new WarnEnterpriseRecord();
                                    BeanUtils.copyProperties(rd, warnEnterpriseRecord);
                                    warnEnterpriseRecord.setYearIsMonth(record.getYearIsMonth());
                                    warnEnterpriseRecord.setWarnFactor("30");
                                    warnEnterpriseRecord.setWarnType(RuleEnum.TRADE_EDIT_IP_FACTOR_DATA.getCode());
                                    warnEnterpriseRecord.setDimension(DimensionEnum.A.getCode());
                                    warnEnterpriseRecord.setMajorClassify(TableNameEnum.S_TYPE.getType());
                                    warnEnterpriseRecord.setWarnCode(generateWarnCode(DimensionEnum.A.getCode(), start, year));
                                    warnEnterpriseRecord.setWarningIndex(String.format("%.1f", 30 * 0.15));
                                    warnEnterpriseRecord.setCreateTime(DateTime.now().toDate());
                                    start++;
                                    res.add(warnEnterpriseRecord);
                                    break;
                                }
                            }
                        }
                        if (!res.isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(res);
                        }
                    } while (!ls.isEmpty());
                    break;
                case services:
                    limit = 0;
                    statTableId = warnEnterpriseRecordMapper.selectStatIdBy(year, TableNameEnum.F_TYPE.getDes());
                    //需要计算风险的企业
                    list = warnEnterpriseRecordMapper.selectSameIpBy("et_" + statTableId, year);
                    log.info(">>>>>>>>>>>>>查询到{}家企业存在风险", list.size());
                    do {
                        res = new ArrayList<>();
                        ls = warnEnterpriseRecordMapper.getService(limit, 100, year);
                        limit += 100;
                        for (WarnEnterpriseRecord record : list) {
                            for (WarnEnterpriseRecord rd : ls) {
                                if (record.getCompanyCode().equals(rd.getCompanyCode())) {
                                    WarnEnterpriseRecord warnEnterpriseRecord = new WarnEnterpriseRecord();
                                    BeanUtils.copyProperties(rd, warnEnterpriseRecord);
                                    warnEnterpriseRecord.setYearIsMonth(record.getYearIsMonth());
                                    warnEnterpriseRecord.setWarnFactor("30");
                                    warnEnterpriseRecord.setWarnType(RuleEnum.SERVICE_EDIT_IP_FACTOR_DATA.getCode());
                                    warnEnterpriseRecord.setDimension(DimensionEnum.A.getCode());
                                    warnEnterpriseRecord.setMajorClassify(TableNameEnum.F_TYPE.getType());
                                    warnEnterpriseRecord.setWarnCode(generateWarnCode(DimensionEnum.A.getCode(), start, year));
                                    warnEnterpriseRecord.setWarningIndex(String.format("%.1f", 30 * 0.15));
                                    warnEnterpriseRecord.setCreateTime(DateTime.now().toDate());
                                    start++;
                                    log.info(">>>>>>>>>>组织机构代码为:{}的企业存在风险", record.getCompanyCode());
                                    res.add(warnEnterpriseRecord);
                                    break;
                                }
                            }
                        }
                        if (!res.isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(res);
                        }
                    } while (!ls.isEmpty());
                    break;
                case investment:
                    limit = 0;
                    statTableId = warnEnterpriseRecordMapper.selectStatIdBy(year, TableNameEnum._206_TYPE.getDes());
                    //需要计算风险的企业
                    list = warnEnterpriseRecordMapper.selectSameIpBy("et_" + statTableId, year);
                    do {
                        res = new ArrayList<>();
                        ls = warnEnterpriseRecordMapper.getInvest(limit, 100, year);
                        limit += 100;
                        outer:
                        for (WarnEnterpriseRecord record : list) {
                            for (WarnEnterpriseRecord rd : ls) {
                                if (record.getCompanyCode().equals(rd.getCompanyCode())) {
                                    WarnEnterpriseRecord warnEnterpriseRecord = new WarnEnterpriseRecord();
                                    BeanUtils.copyProperties(rd, warnEnterpriseRecord);
                                    warnEnterpriseRecord.setYearIsMonth(record.getYearIsMonth());
                                    warnEnterpriseRecord.setWarnFactor("30");
                                    warnEnterpriseRecord.setWarnType(RuleEnum.INVEST_EDIT_IP_FACTOR_DATA.getCode());
                                    warnEnterpriseRecord.setDimension(DimensionEnum.A.getCode());
                                    warnEnterpriseRecord.setMajorClassify(TableNameEnum._206_TYPE.getType());
                                    warnEnterpriseRecord.setWarnCode(generateWarnCode(DimensionEnum.A.getCode(), start, year));
                                    warnEnterpriseRecord.setWarningIndex(String.format("%.1f", 30 * 0.15));
                                    warnEnterpriseRecord.setCreateTime(DateTime.now().toDate());
                                    warnEnterpriseRecord.setProjectCode(record.getProjectCode());
                                    warnEnterpriseRecord.setProjectName(record.getProjectName());
                                    start++;
                                    res.add(warnEnterpriseRecord);
                                    break outer;
                                }
                            }
                        }
                        if (!res.isEmpty()) {
                            warnEnterpriseRecordMapper.insertBatch(res);
                        }
                    } while (!ls.isEmpty());
                    break;
                default:
                    break;
            }
        }
        return 0;
    }



    private String generateWarnCode(String dimension, Integer start, String year) {
        NumberFormat format = NumberFormat.getInstance();
        format.setMinimumIntegerDigits(8);
        format.setGroupingUsed(false);
        return dimension + year + format.format(start);
    }

    private String generateSnowFlake() {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);
        return snowflake.nextIdStr();
    }

    private Double generateWarnIndex(String key, String factor) {
        //System.out.println(">>>>>>>>>"+key);
        //System.out.println(">>>>>>>>>"+factor);
        Double weight = EnumUtils.getEnum(RuleEnum.class, key.toUpperCase()).getWeight();
        return Double.parseDouble(factor) * weight;
    }

    private int find(String companyCode, List<WarnEnterpriseRecord> lst) {
        int res = -1;
        for (int i = 0; i < lst.size(); i++) {
            if (lst.get(i).getCompanyCode().equals(companyCode)) {
                res = i;
                break;
            }
        }
        return res;
    }

}
